<?php

namespace addons\official\controller\api;

use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Config;
use think\Validate;

/**
 * 会员接口
 */
class User extends BaseApi
{
    protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'changeemail', 'changemobile', 'third'];
    protected $noNeedRight = '*';


    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__api('','User center already closed'));
        }

    }



    /**
     * 会员登录
     *
     * @ApiMethod (POST)
     * @param string $account  账号
     * @param string $password 密码
     */
    public function login()
    {
        $account = $this->request->post('account');
        $password = $this->request->post('password');
        if (!$account || !$password) {
            $this->error(__api('','Invalid parameters'));
        }
        $ret = $this->auth->login($account, $password);
        if ($ret) {
            //$data = ['userinfo' => $this->auth->getUserinfo()];
            $user = db('user')->where('id',$this->auth->id)->find();
            $user['avatar'] = empty($user['avatar'])?letter_avatar($user['username']):'';
            $user['prevtime'] = $user['prevtime']?date('Y-m-d H:i:s',$user['prevtime']):'';
            $user['logintime'] = $user['logintime']?date('Y-m-d H:i:s',$user['logintime']):'';
            $user['jointime'] = $user['jointime']?date('Y-m-d H:i:s',$user['jointime']):'';
            $user['createtime'] = $user['createtime']?date('Y-m-d H:i:s',$user['createtime']):'';
            $user['updatetime'] = $user['updatetime']?date('Y-m-d H:i:s',$user['updatetime']):'';
            $user['token'] = $this->auth->getToken();
            $data = ['userinfo' => $user];
            $this->success(__api('','Logged in successful'), $data);
        } else {
            $this->error($this->auth->getError());
        }
    }


    /**
     * 注册会员
     *
     * @ApiMethod (POST)
     * @param string $username 用户名
     * @param string $password 密码
     * @param string $email    邮箱
     * @param string $mobile   手机号
     */
    public function register()
    {
        $username = $this->request->post('username');
        $password = $this->request->post('password');
        $email = $this->request->post('email');
        $mobile = $this->request->post('mobile');
        if (!$username || !$password) {
            $this->error(__api('user','Invalid parameters'));
        }
        if ($email && !Validate::is($email, "email")) {
            $this->error(__api('user','Email is incorrect'));
        }
        if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
            $this->error(__api('user','Mobile is incorrect'));
        }
        $ret = $this->auth->register($username, $password, $email, $mobile, []);
        if ($ret) {
            //$data = ['userinfo' => $this->auth->getUserinfo()];
            $user = db('user')->where('id',$this->auth->id)->find();
            $user['avatar'] = empty($user['avatar'])?letter_avatar($user['username']):'';
            $user['prevtime'] = $user['prevtime']?date('Y-m-d H:i:s',$user['prevtime']):'';
            $user['logintime'] = $user['logintime']?date('Y-m-d H:i:s',$user['logintime']):'';
            $user['jointime'] = $user['jointime']?date('Y-m-d H:i:s',$user['jointime']):'';
            $user['createtime'] = $user['createtime']?date('Y-m-d H:i:s',$user['createtime']):'';
            $user['updatetime'] = $user['updatetime']?date('Y-m-d H:i:s',$user['updatetime']):'';
            $user['token'] = $this->auth->getToken();
            $data = ['userinfo' => $user];
            $this->success(__api('user','Sign up successful'), $data);
        } else {
            $this->error($this->auth->getError());
        }
    }

    /**
     * 退出登录
     * @ApiMethod (POST)
     */
    public function logout()
    {
        if (!$this->request->isPost()) {
            $this->error(__api('','Invalid parameters'));
        }
        $this->auth->logout();
        $this->success(__api('','Logout successful'));
    }

    /**
     * 修改会员个人信息
     *
     * @ApiMethod (POST)
     * @param string $nickname    昵称
     * @param string $email       电子邮箱
     * @param string $mobile      手机号
     * @param string $gender      性别
     * @param string $birthday    生日
     */
    public function profile()
    {
        $data = input('post.');
        if (isset($data['uni_language'])){
            unset($data['uni_language']);
        }
        $user = db('user')->where('id',$this->auth->id)->update($data);
        if ($user){
            $user = db('user')->where('id',$this->auth->id)->find();
            $user['avatar'] = empty($user['avatar'])?letter_avatar($user['username']):'';
            $user['prevtime'] = $user['prevtime']?date('Y-m-d H:i:s',$user['prevtime']):'';
            $user['logintime'] = $user['logintime']?date('Y-m-d H:i:s',$user['logintime']):'';
            $user['jointime'] = $user['jointime']?date('Y-m-d H:i:s',$user['jointime']):'';
            $user['createtime'] = $user['createtime']?date('Y-m-d H:i:s',$user['createtime']):'';
            $user['updatetime'] = $user['updatetime']?date('Y-m-d H:i:s',$user['updatetime']):'';
            $user['token'] = $this->auth->getToken();
            $data = ['userinfo' => $user];
            $this->success(__api('','Operation completed'),$data);
        }
        $this->error(__api('','Operation failed'));
    }





    /**
     * 重置密码
     * @ApiMethod (POST)
     * @param string $newpassword 新密码
     */
    public function resetpwd()
    {
        $newpassword = $this->request->post("newpassword");
        $ret = 0;
        if ($newpassword){
            $ret = $this->auth->changepwd($newpassword, '', true);
        }
        if ($ret) {
            $this->success(__api('','Reset password successful'));
        } else {
            $this->error($this->auth->getError());
        }
    }
}
